
from qturboframe.lib.webdriver.exceptions import SeleniumError
from qturboframe.lib.report.log import logger
from qturboframe.public_ui.h_ui.menu import Menu as HuiMenu
from .home_tab import HomeTab
from qturboframe.public_ui.binding_menu_options import binding_menu_vue
from qturboframe.lib.common.web_config import web_config


class Menu(HuiMenu):
    '''
    选择到详细菜单页面。
    '''

    @binding_menu_vue
    def select(self, linkText):
        '''
        风险合规的产品：菜单栏菜单选择

        :Args:
         - linkText: 菜单栏链接名称集合，按照顺序点击链接

        :Raises:
            SeleniumError: 菜单操作失败
        '''
        try:
            valuelist = linkText.split('/')
            # 判断菜单页面是否已经打开，打开则刷新，未打开则打开菜单
            page_tabs = self._driver.find_elements_by_attribute('css selector', 'a.tabs-view')
            page_tabs_list = list()
            [page_tabs_list.append(page_tab.text) for page_tab in page_tabs]
            open_page_tab = self._driver.exist('css selector', 'a.tabs-view.router-link-active', timeout=0.1)
            page_name = open_page_tab.text
            # 当已经在菜单栏页面时进行刷新，否则重新进入菜单(弹框判断)
            if valuelist[-1] == page_name:
                if len(page_tabs_list) != 2:
                    HomeTab(valuelist[-1], self._driver).close_others()
                logger.info('打开菜单：{}'.format(valuelist[-1]))
                HomeTab(valuelist[-1], self._driver).reload()
            else:
                # 第一级菜单选择
                first_menu_ul = self._element.find_element_by_attribute('xpath', "//div[@class='list-item']", 'text:' + valuelist[0], timeout=self.timeout)
                first_menu_ul.focus()
                first_menu_ul.move_to()
                logger.info('选择第一级菜单：{}'.format(valuelist[0]))
                # 第二级菜单选择
                menu_tab = first_menu_ul.find_element_by_attribute('xpath', 'following-sibling::div[@class="list-tab"]', timeout=self.timeout)
                menu_box = menu_tab.find_element_by_attribute('css selector', 'div.list-menu>div.list-container>div.list-box', timeout=self.timeout)
                second_menu = menu_box.find_element_by_attribute('css selector', 'li.item>div.list-box-indent>div.menuLeaf>span', 'title:' + valuelist[1], timeout=self.timeout)
                second_menu.focus()
                second_menu.click()
                logger.info('选择第二级菜单：{}'.format(valuelist[1]))
            self.check_open_menu(valuelist[-1], page_tabs_list)
        except Exception as msg:
            raise SeleniumError(str(msg))

    def check_open_menu(self, name, page_tabs_list=None):
        '''
        判断菜单是否已经被打开
        :param name: 菜单名称
        :param page_tabs_list:
        :return:
        '''
        web_config.last_menu = name
        # 当前菜单打开但是当前页面不为当前菜单，进行刷新
        if page_tabs_list and name in page_tabs_list:
            HomeTab(name, self._driver).reload()
        # 确认菜单已经打开
        timeout = 0
        while timeout < self.timeout:
            if HomeTab(name, self._driver).element:
                logger.debug('{} 菜单页已经打开！'.format(name))
                break
            else:
                timeout = timeout + 0.1
        else:
            raise SeleniumError('未打开{} 菜单页！'.format(name))


